﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TanmiaGrp.Basis.Core;
using TanmiaGrp.GeneralLedger.Core;
using TanmiaGrp.GeneralLedger.IRepository;
using RefactorThis.GraphDiff;

namespace TanmiaGrp.GeneralLedger.SqlServerRepository
{
    public class AccountRepository : DbRepository, IAccountRepository
    {
        //public override bool Update<T>(T entity)
        //{
        //    Account updatedAccount = entity as Account;
        //    Account existingAccount = GLContext.Current.Accounts
        //        .AsNoTracking()
        //        .Include(x => x.AccountGroups)
        //        .FirstOrDefault(x => x.AccountID == updatedAccount.AccountID);

        //    var existingGroups = existingAccount.AccountGroups.ToList();

        //    var updatedGroups = updatedAccount.AccountGroups.ToList();

        //    var addedGroups = updatedGroups.Except(existingGroups);

        //    var deletedGroups = existingGroups.Except(updatedGroups);

        //    GLContext.Current.Set<T>().Attach(entity);
        //    GLContext.Current.Entry(entity).State = EntityState.Modified;

        //    addedGroups.ToList().ForEach(x =>
        //    {
        //        GLContext.Current.ObjectStateManager.ChangeRelationshipState(entity, x, "AccountGroups", EntityState.Added);
        //    });

        //    deletedGroups.ToList().ForEach(x =>
        //    {
        //        GLContext.Current.Groups.Attach(x);
        //        GLContext.Current.Entry(x).State = EntityState.Unchanged;
        //        GLContext.Current.ObjectStateManager.ChangeRelationshipState(entity, x, "AccountGroups", EntityState.Deleted);
        //    });


        //    int erc = GLContext.Current.SaveChanges();

        //    return erc > 0;
        //}
        #region IDbRepository<Account> Members

        public bool Create(Account entity)
        {
            return base.Create(entity);
        }

        public new bool Update(Account entity)
        {
            throw new NotImplementedException("Commented Intentionally");
            //using (GLContext context = new GLContext())
            //{
            //    context.UpdateGraph(entity);
                
            //    context.Accounts.Attach(entity.Parent);

            //    foreach (var item in entity.AccountGroups)
            //    {
            //        context.Groups.Attach(item);
            //    }

            //    context.Entry<Account>(entity).State = EntityState.Modified;

            //    Type entityType = entity.GetType();
            //    context.Set(entityType).Add(entity);

            //    //context.Entry(entity).State = EntityState.Modified;

            //    context.ChangeTracker.DetectChanges();
            //    object obj = context.ChangeTracker.Entries();

            //    obj = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);

            //    foreach (var property in entity.GetAssociations(AssociationTypeEnum.Aggregation))
            //    {
            //        IEnumerable bizObjCollection = property.GetBusinessObjectCollection(entity);

            //        foreach (var item in bizObjCollection.GetStateList("Added"))
            //        {
            //            context.ObjectStateManager.ChangeRelationshipState(entity, item, property.Name, EntityState.Added);
            //        }

            //        foreach (var item in bizObjCollection.GetStateList("Removed"))
            //        {
            //            context.Set(item.GetType()).Attach(item);
            //            context.Entry(item).State = EntityState.Unchanged;

            //            context.ObjectStateManager.ChangeRelationshipState(entity, item, property.Name, EntityState.Deleted);
            //        }
            //    }

            //    int erc = context.SaveChanges();

            //    return erc > 0;

            //    Account updatedAccount = entity as Account;
            //    Account existingAccount = context.Accounts
            //        .AsNoTracking()
            //        .Include(x => x.AccountGroups)
            //        .FirstOrDefault(x => x.AccountID == updatedAccount.AccountID);

            //    var existingGroups = existingAccount.AccountGroups.ToList();

            //    var updatedGroups = updatedAccount.AccountGroups.ToList();

            //    var addedGroups = updatedGroups.Except(existingGroups);

            //    var deletedGroups = existingGroups.Except(updatedGroups);

            //    context.Accounts.Attach(entity);
            //    context.Entry(entity).State = EntityState.Modified;

            //    addedGroups.ToList().ForEach(x =>
            //    {
            //        context.ObjectStateManager.ChangeRelationshipState(entity, x, "AccountGroups", EntityState.Added);
            //    });

            //    deletedGroups.ToList().ForEach(x =>
            //    {
            //        context.Groups.Attach(x);
            //        context.Entry(x).State = EntityState.Unchanged;
            //        context.ObjectStateManager.ChangeRelationshipState(entity, x, "AccountGroups", EntityState.Deleted);
            //    });

            //    erc = context.SaveChanges();

            //    return erc > 0;
            //}
        }

        public bool Delete(Account entity)
        {
            return base.Delete(entity);
        }

        public bool Delete(IEnumerable<Account> entities)
        {
            return base.Delete(entities);
        }

        #endregion
    }
}
